#include "RingQueue.hpp"
#include "Task.hpp"
#include<unistd.h>

class threadData
{
public:
    RingQueue<Task> *rq_;
    string threadname_;
};
void *Consumer(void *args)
{
    threadData *td = static_cast<threadData *>(args);
     string name = td->threadname_;
    RingQueue<Task> *rq = td->rq_;
    while(true){
        Task task;
        rq->pop(&task);
        task();
        cout << name << "处理了一个数据:" << task.getresult()<<endl;
        sleep(1);
    }
}

void *Productor(void *args)
{
    threadData *td = static_cast<threadData *>(args);
    string name = td->threadname_;
    RingQueue<Task> *rq = td->rq_;
    while (true)
    {
        int data1 = rand() % 10 + 1;
        int data2 = rand() % 10 + 1;
        int len = opers.size();
        int oper = rand() % len;
        char op = opers[oper];
        Task task(data1, data2, op);
        rq->push(task);
        cout << name << "生产了一个数据:" << task.getTask()<<endl;
    }
}

int main()
{
    srand(time(nullptr));
    RingQueue<Task> *rq=new RingQueue<Task>();
    pthread_t c[5], p[3];
    for (int i = 0; i < 1; i++)
    {
        threadData *T = new threadData();
        T->rq_ = rq;
        T->threadname_ = "thread" + to_string(i);
        pthread_create(p + i, nullptr, Productor, T);
    }
    for (int i = 0; i < 1; i++)
    {
        threadData *T = new threadData();
        T->rq_ = rq;
        T->threadname_ = "thread" + to_string(i);
        pthread_create(c + i, nullptr, Consumer, T);
    }
    for (int i = 0; i < 1; i++)
    {
        pthread_join(c[i], nullptr);
    }
    for (int i = 0; i < 1; i++)
    {
        pthread_join(p[i], nullptr);
    }
}